/*********************************************************
          File Name:9.28.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Thu 18 Feb 2016 08:34:09 PM CST
**********************************************************/

#include <iostream>
#include <forward_list>
#include <string>

using namespace std;

int insert(forward_list<string> &fs, const string &s1, const string s2)
{
    auto p = fs.before_begin();
    auto iter = fs.begin();
    for(; iter != fs.end();)
    {
        if(*iter == s1)
        {
            iter = fs.insert_after(iter, s2);
            return 0;
        }
        p = iter;
        ++iter;
    }
    fs.insert_after(p, s2);
    return -1;
}

int main()
{
    forward_list<string> fs;

    if(insert(fs, "this", "is") != 0)
        cout << "append\n";
    if(insert(fs, "is", "a test") == 0)
        cout << "insert after\n";

    for(auto x: fs)
        cout << x << endl;

    return 0;
}
